/*******************************************************************************
* Piotr's Computer Vision Matlab Toolbox      Version 3.00
* Copyright 2014 Piotr Dollar.  [pdollar-at-gmail.com]
* Licensed under the Simplified BSD License [see external/bsd.txt]
*******************************************************************************/
#ifndef _WRAPPERS_HPP_
#define _WRAPPERS_HPP_

#include <stdlib.h>

#ifdef MATLAB_MEX_FILE


// wrapper functions if compiling from Matlab
#include "mex.h"
inline void wrError(const char *errormsg) { mexErrMsgTxt(errormsg); }
inline void* wrCalloc( size_t num, size_t size ) { return mxCalloc(num,size); }
inline void* wrMalloc( size_t size ) { return mxMalloc(size); }
inline void wrFree( void * ptr ) { mxFree(ptr); }

#else

// wrapper functions if compiling from C/C++
inline void wrError(const char *errormsg) { throw errormsg; }

inline void *wrCalloc(size_t num, size_t size) { return calloc(num, size); }

inline void *wrMalloc(size_t size) { return malloc(size); }

inline void wrFree(void *ptr) { free(ptr); }

#endif

// platform independent aligned memory allocation (see also alFree)
inline void *alMalloc(size_t size, int alignment) {
    const size_t pSize = sizeof(void *), a = alignment - 1;
    void *raw = wrMalloc(size + a + pSize);
    void *aligned = (void *) (((size_t) raw + pSize + a) & ~a);
    *(void **) ((size_t) aligned - pSize) = raw;
    return aligned;
}

// platform independent alignned memory de-allocation (see also alMalloc)
inline void alFree(void *aligned) {
    void *raw = *(void **) ((char *) aligned - sizeof(void *));
    wrFree(raw);
}

#endif
